q()
dev.off()
rm(list = ls())
rstudioapi::getActiveDocumentContext
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
library(ggplot2)
library(plyr)
library(reshape2)
library(scales)
library(grid)
library(gridExtra)
#Figure 1: Distribution of Government Responsiveness and Punishment across Scandal Types  (Color)
data_f1=read.table("04_Figure_discipline.csv", header=TRUE,sep=",")
mdata_f1=melt(data_f1,id=c("type","id"))
mdata_f1$type=factor(mdata_f1$type, levels=c(1,2,3,4), labels=c("Economic Crimes", "Dereliction of Duty","Sex Scandals","Inappropriate Speech"))
mdata_f1$variable=revalue(mdata_f1$variable, c("No.Action"=0,"Missing"=1, "Administrative.Penalty"=2, "Judicial.Penalty"=3, "Promotion"=4))
mdata_f1$variable=factor(mdata_f1$variable, levels=c(0,1,2,3,4), labels=c("No Action","Missing","Administrative Penalty","Judicial Penalty","Promotion"))
data_f2=read.table("04_Figure_response.csv", header=TRUE,sep=",")
mdata_f2=melt(data_f2,id=c("type","id"))
mdata_f2$type=factor(mdata_f2$type, levels=c(1,2,3,4), labels=c("Economic Crimes", "Dereliction of Duty","Sex Scandals","Inappropriate Speech"))
mdata_f2$variable=revalue(mdata_f2$variable, c("withinone"=1, "notwithin"=2))
mdata_f2$variable=factor(mdata_f2$variable, levels=c(1,2), labels=c("Government responds within one month", "Government does not respond within one month"))
mdata_f2$variable=factor(mdata_f2$variable, levels=rev(levels(mdata_f2$variable)))
par(mfrow=c(1,2))
f1=ggplot(mdata_f1,aes(x=type,y=value,fill=variable))+
geom_bar(position="fill",stat="identity") +
scale_fill_manual(values=c("#F8766D","#CC9933","#00BA38","#619CFF","#C77CFF"))+
scale_y_continuous(labels=percent_format())+
xlab("") +
ylab("") +
ggtitle("(B)") +
theme(legend.position="bottom")+
guides(fill = guide_legend(nrow = 2))+
theme(legend.title=element_blank())+
theme(plot.title=element_text(hjust=0.5))
f2=ggplot(mdata_f2,aes(x=type,y=value,fill=variable))+
geom_bar(position="fill",stat="identity") +
scale_fill_manual(values=c("#00BFC4","#F8766D"))+
scale_y_continuous(labels=percent_format())+
ggtitle("(A)") +
xlab("") +
ylab("") +
theme(legend.position="bottom")+
guides(fill = guide_legend(nrow = 2))+
theme(legend.title=element_blank())+
theme(plot.title=element_text(hjust=0.5))
pdf("Figure 1.pdf", width=10, height=4.5)
grid.arrange(f2,f1, ncol = 2)
dev.off()
#Figure 2
mydata=read.table("04_Restricted_Sample.csv", header=TRUE,sep=",")
mydata$place=factor(mydata$place, levels=c(1,2), labels=c("Effects on the Speed of Government Response", "Effects on Punishment Severity"))
mydata$type=factor(mydata$type, levels=c(1,2), labels=c("(A) Restricted Sample of Lower-level Officials", "(B) Restricted Sample of Executive/Functional Government Officials"))
pdf("Figure 2.pdf", width=12, height=4.5)
ggplot(mydata,aes(x=place,y=coefficient))+
facet_grid(. ~ type) + # split plot after error bar type
geom_point(size=2)+
geom_errorbar(aes(ymax=CI95up,ymin=CI95down),width = 0.05)+
scale_y_continuous(limits = c(-0.05, 0.20))+
xlab("") +
ylab("") +
theme(text = element_text(size=12))
dev.off()
#Figure A2
samplepost=read.table("04_Figure_Distribution.csv", header=TRUE,sep=",")
samplepost$date <- factor(samplepost$date, levels=samplepost$date[!duplicated(samplepost$date)])
levels(samplepost$date)
samplepost$date=as.Date(samplepost$date, "%m/%d/%Y")
pdf("Figure A2.pdf",width=10, height=6)
ggplot(samplepost,aes(x=date,y=sample.posts))+
geom_line()+
scale_x_date() + xlab("") + ylab("Number of Sample Posts")+ scale_y_continuous(labels=comma)
dev.off()
#Figure A3
samplepost_com=read.table("04_Figure_Comparison.csv", header=TRUE,sep=",")
samplepost_com$date <- factor(samplepost_com$date, levels=samplepost_com$date[!duplicated(samplepost_com$date)])
levels(samplepost_com$date)
samplepost_com$date=as.Date(samplepost_com$date, "%m/%d/%Y")
samplepost_com$Date.Users <- factor(samplepost_com$Date.Users, levels=samplepost_com$Date.Users[!duplicated(samplepost_com$Date.Users)])
levels(samplepost_com$Date.Users)
samplepost_com$Date.Users=as.Date(samplepost_com$Date.Users, "%m/%d/%Y")
par(mfrow=c(2,1))
p1=ggplot(samplepost_com,aes(x=date))+
geom_line(aes(y=sample.posts))+
xlab("")+
ylab("Number of Sample Posts")+
scale_y_continuous(labels=comma)
p2=ggplot(samplepost_com,aes(x=Date.Users))+
geom_line(aes(y=Number.of.Weibo.Users))+
xlab("")+
ylab("Number of Weibo Users (Million)")+
scale_y_continuous(labels=comma)
pdf("Figure A3.pdf",width=12, height=4.5)
grid.arrange(p1, p2, ncol = 2,bottom=textGrob("Source: Our Dataset and Sina Corporation Quarterly Reports"))
dev.off()
savehistory("C:/Users/yiranli/Dropbox/Ongoing_Project/Scandal/PSRM/Replication/R/05_analysis.Rhistory")
